package com.cold.legendary.bprocess.context;

import com.cold.legendary.bprocess.common.constance.BErrorCodeEnum;
import com.cold.legendary.bprocess.common.exception.BProcessException;
import com.cold.legendary.bprocess.context.manager.BProcessManager;
import com.cold.legendary.bprocess.image.manager.BProcessImageManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * @author cold
 * @since 2023/3/9
 */
@Slf4j
@Component
public class ProcessContextApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //开始加载loan-process
        log.info("loan-process开始初始化...");
        ApplicationContext applicationContext = SpringApplicationContextHolder.getApplicationContext();
        if (null == applicationContext) {
            throw new BProcessException(BErrorCodeEnum.SYS_CONTEXT_ERROR);
        }
        //加载节点定义信息
        log.info("loan-process开始加载points...");
        BProcessManager.loadPoints(applicationContext);
        //构建process流程信息
        log.info("loan-process开始加载构建process...");
        BProcessManager.loadProcesses(applicationContext);

        log.info("loan-process开始加载Sequence自定义实现器...");
        BProcessManager.loadSequenceClazzDef(applicationContext);

        log.info("loan-process开始加载GraphicInfos...");
        BProcessImageManager.loadGraphicInfos();

        log.info("loan-process完成初始化...");
    }
}
